/*
 * Copyright (c) 2018. J4dream Created.
 */

package com.j4dream.property.service.implment;

import com.google.common.collect.ImmutableMap;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.j4dream.property.config.PropertiesConfig;
import com.j4dream.property.service.EmailService;
import freemarker.template.Template;
import org.apache.http.client.utils.URIBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.annotation.PostConstruct;
import javax.mail.internet.MimeMessage;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

@Service
public class EmailServiceDefaultImplement implements EmailService {

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private PropertiesConfig.EmailConfig emailConfig;

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    private Executor executor;

    @PostConstruct
    public void initData() {
        ThreadFactory factory = new ThreadFactoryBuilder()
                .setNameFormat("mail-pool-%d")
                .build();
        executor = Executors.newCachedThreadPool(factory);
    }

    @Override
    public void sendActiveEmail() {
        //sendMail();
    }

    @Override
    public void sendResetPasswordEmail(String recipient, String token) {

        String url = String.format(emailConfig.getResetPassUrl(), token);

        Map<Object, Object> data = ImmutableMap.builder()
                .put("resetPassUrl", url)
                .build();

        NotificationEmail tplData = NotificationEmail.builder()
                .subject("重置密码")
                .recipient(recipient)
                .template("reset-pass.ftl")
                .data(data)
                .build();
        sendMail(tplData);
    }

    @Override
    public void sendMail(NotificationEmail tplData) {
        try {
            //SimpleMailMessage message = new SimpleMailMessage();
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setFrom(emailConfig.getSender());
            Template template = freeMarkerConfigurer.getConfiguration().getTemplate(tplData.getTemplate());
            String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, tplData.getData());

            mimeMessageHelper.setTo(tplData.getRecipient());
            mimeMessageHelper.setSubject(tplData.getSubject());
            mimeMessageHelper.setText(html, true);

            executor.execute( () -> javaMailSender.send(mimeMessage));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }


}
